package com.member.shop.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.member.common.utils.DateUtils;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.member.shop.vo.GoodsBindVo;
import org.springframework.stereotype.Service;
import com.member.shop.mapper.SysBizRecommendGoodsMapper;
import com.member.shop.domain.SysBizRecommendGoods;
import com.member.shop.service.ISysBizRecommendGoodsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 推荐分类与商品管理Service业务层处理
 *
 * @author lixinchen
 * @date 2025-01-08
 */
@Service
public class SysBizRecommendGoodsServiceImpl extends ServiceImpl<SysBizRecommendGoodsMapper, SysBizRecommendGoods> implements ISysBizRecommendGoodsService {
    @Resource
    private SysBizRecommendGoodsMapper sysBizRecommendGoodsMapper;

    /**
     * 查询推荐分类与商品管理
     *
     * @param id 推荐分类与商品管理主键
     * @return 推荐分类与商品管理
     */
    @Override
    public SysBizRecommendGoods selectSysBizRecommendGoodsById(Long id) {
        return sysBizRecommendGoodsMapper.selectById(id);
    }

    /**
     * 查询推荐分类与商品管理列表
     *
     * @param queryWrapper 推荐分类与商品管理
     * @return 推荐分类与商品管理
     */
    @Override
    public List<SysBizRecommendGoods> selectSysBizRecommendGoodsList(LambdaQueryWrapper<SysBizRecommendGoods> queryWrapper) {
        return sysBizRecommendGoodsMapper.selectList(queryWrapper);
    }

    @Override
    public List<SysBizRecommendGoods> selectSysBizRecommendGoodsList(SysBizRecommendGoods sysBizRecommendGoods) {
        return sysBizRecommendGoodsMapper.selectSysBizRecommendGoodsList(sysBizRecommendGoods);
    }

    /**
     * 新增推荐分类与商品管理
     *
     * @param sysBizRecommendGoods 推荐分类与商品管理
     * @return 结果
     */
    @Override
    public int insertSysBizRecommendGoods(SysBizRecommendGoods sysBizRecommendGoods) {
        sysBizRecommendGoods.setCreateTime(DateUtils.getNowDate());
        return sysBizRecommendGoodsMapper.insert(sysBizRecommendGoods);
    }

    /**
     * 修改推荐分类与商品管理
     *
     * @param sysBizRecommendGoods 推荐分类与商品管理
     * @return 结果
     */
    @Override
    public int updateSysBizRecommendGoods(SysBizRecommendGoods sysBizRecommendGoods) {
        sysBizRecommendGoods.setUpdateTime(DateUtils.getNowDate());
        return sysBizRecommendGoodsMapper.updateById(sysBizRecommendGoods);
    }

    /**
     * 批量删除推荐分类与商品管理
     *
     * @param ids 需要删除的推荐分类与商品管理主键
     * @return 结果
     */
    @Override
    public int deleteSysBizRecommendGoodsByIds(Long[] ids) {
        return sysBizRecommendGoodsMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除推荐分类与商品管理信息
     *
     * @param id 推荐分类与商品管理主键
     * @return 结果
     */
    @Override
    public int deleteSysBizRecommendGoodsById(Long id) {
        return sysBizRecommendGoodsMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int bindGoods(GoodsBindVo goodsBindVo) {
        if (goodsBindVo == null || goodsBindVo.getGoodsIds() == null || goodsBindVo.getGoodsIds().isEmpty()) {
            return 0; // 如果没有商品 ID 列表，则直接返回
        }

        Long recommendCategoryId = goodsBindVo.getRecommendCategoryId();
        if (recommendCategoryId == null) {
            throw new IllegalArgumentException("推荐类别 ID 不能为空");
        }

        // 删除指定推荐类别下的所有商品推荐记录
        sysBizRecommendGoodsMapper.delete(new LambdaQueryWrapper<SysBizRecommendGoods>()
                .eq(SysBizRecommendGoods::getRecommendCategoryId, recommendCategoryId));

        // 构造批量插入的数据列表
        List<SysBizRecommendGoods> recommendGoodsList = goodsBindVo.getGoodsIds().stream()
                .map(goodsId -> {
                    SysBizRecommendGoods sysBizRecommendGoods = new SysBizRecommendGoods();
                    sysBizRecommendGoods.setRecommendCategoryId(recommendCategoryId);
                    sysBizRecommendGoods.setTenantId(goodsBindVo.getTenantId());
                    sysBizRecommendGoods.setGoodsId(goodsId);
                    return sysBizRecommendGoods;
                })
                .collect(Collectors.toList());

        // 批量插入新的商品推荐记录
        int insertCount = sysBizRecommendGoodsMapper.batchInsert(recommendGoodsList);

        return insertCount; // 返回插入的记录数
    }
}
